package com.sky.Database;

import java.util.ArrayList;
import java.util.List;

import com.sky.Database.model.location;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;


public class locationSQL {

	private SQLiteDatabase newDB;
	private List<location> locationList;

	public locationSQL(DBHelper dbHelper) {
		newDB = dbHelper.openDataBase();
	}

	public void retrieveLocationList() {
		locationList = new ArrayList<location>();
		Cursor c = null;

		try {
			c = newDB.rawQuery("SELECT * FROM location " + " order by id", null);
			if (c != null) {
				if (c.moveToFirst()) {
					do {
						location local = new location();
						local.setId(c.getInt(c.getColumnIndex("id")));
						local.setName(c.getString(c.getColumnIndex("Name")));
					
						local.setAddress(c.getString(c.getColumnIndex("adress")));
						
						local.setPhone_num(c.getString(c.getColumnIndex("phone_num")));				
						local.setFax(c.getString(c.getColumnIndex("fax")));
						local.setMap(c.getString(c.getColumnIndex("map")));

						locationList.add(local);
					} while (c.moveToNext());
				}
			}
		} catch (SQLiteException se) {
			Log.e(getClass().getSimpleName(), "Could not create or Open the database");
		} finally {
			DBHelper.closeCursor(c);
		}
	}

	public location getByID(long id) {
		Cursor c = null;

		try {
			c = newDB.rawQuery("SELECT * FROM location " + " WHERE id = " + id, null);
			if (c != null) {
				if (c.moveToFirst()) {
					do {
						location local = new location();
						local.setId(c.getInt(c.getColumnIndex("id")));
						local.setName(c.getString(c.getColumnIndex("name")));
					
						local.setAddress(c.getString(c.getColumnIndex("address")));
						
						local.setPhone_num(c.getString(c.getColumnIndex("phone_num")));				
						local.setFax(c.getString(c.getColumnIndex("fax")));
						local.setMap(c.getString(c.getColumnIndex("map")));
						return local;
					} while (c.moveToNext());
				}
			}
		} catch (SQLiteException se) {
			Log.e(getClass().getSimpleName(), "Could not create or Open the database");
		} finally {
			DBHelper.closeCursor(c);
		}
		return null;
	}

	public List<location> getOfficeList() {
		if (locationList == null) {
			retrieveLocationList();
		}
		return locationList;
	}
	
}
